import javax.swing.JPanel;


public class NewGame {

	private GamePanel display;
	private GameStart startInfo;
	private Player[] playerList;
	private String mapType;
	private String difficulty;
	private GameMap map;
	private MapPanel mapp;
	private State currentS;
	public enum State {landGrant, landBuy, playerTurn, Summary};
	private int currentNP;
	private int NofT;
	//public final int NofP;
	
	/**
	 * Setting up a new game.
	 * */
	public NewGame(GamePanel disp){
		display = disp;
		startInfo = new GameStart(display, this);
	}
	
	/**
	 * Starting a game, setting all the elements to default
	 * */
	public void StartGame(){
		playerList = startInfo.getPlayerList();
		mapType = startInfo.getMap();
		difficulty = startInfo.getDifficulty();
		map = new GameMap(mapType, this);
		mapp = map.getPanel();
		display.changeDisp(mapp);
		
		currentS = State.landGrant;
		currentNP = 0;
		NofT = 0;		
	}
	
	/**
	 * @return current which player 
	 * */
	public Player getCurrentPlayer(){
		return playerList[currentNP];
	}
	
	/**
	 * Setting up next player.
	 * */
	public void nextPlayer(){
		currentNP ++;
		if (currentNP == playerList.length){
			currentNP = 0;
			nextTurn();
		}
	}
	
	/**
	 * Preparing for next turn, setting default 
	 * */
	private void nextTurn(){
		NofT ++;
		if(currentS == State.landGrant){
			if(NofT == 2){
				NofT = 0;
				nextState();
			}
		}
		else if(currentS == State.landBuy){
			Boolean pass = mapp.PassLandBuy();
			if(pass){
				nextState();
			}else{
				mapp.setPassLB();
			}
			// should do nothing
		}else{
			// other state to do
		}
	}
	
	/**
	 * From current to next state.
	 * */
	private void nextState(){
		if(currentS == State.landGrant){
			currentS = State.landBuy;
			mapp.setPassLB();
		}
		else if(currentS == State.landBuy) {
			currentS = State.playerTurn;
			officialStart();
		}
		else {
			//left for next time
		}
		System.out.println("enter next state " + currentS);
	}
	
	/**
	 * @return current state
	 * */
	public NewGame.State getState(){
		return currentS;
	}
	
	/**
	 * @return player list
	 * */
	public Player[] getPlayers(){
		return playerList;
	}
	
	/**
	 * After turn, repaint each tile.
	 * */
	public void officialStart(){
		display.addMapPanel(mapp);
		mapp.repaint();
	}
	
	public void playerEnterTown(){
		display.changeDisp(new TownPanel(getCurrentPlayer(), this));
	}
	
	public void playerOutTown(){
		display.changeDisp(mapp);
	}
}